package com.wds.stock.stockana;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baiwang.bwbaselib.BaseActivity;
import com.baiwang.bwbaselib.ToolBarHelper;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private ViewPager mViewPager;//页卡内容
    private ImageView mTabLine;// 动画图片
    private TextView mTab1,mTab2,mTab3,mTab4;
    private List<Fragment> mFragments;// Tab页面列表
    private int currIndex = 0;// 当前页卡编号
    private int bmpW;// 动画图片宽度
    private Fragment mFragment1,mFragment2,mFragment3,mFragment4;//各个页卡

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setCollapsingToolbar(ToolBarHelper.DRAWER_MODE);
        initTabLine();
        initTabs();
        initViewPager();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {

            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initViewPager() {
        mViewPager=(ViewPager) findViewById(R.id.vPager);
        mFragments=new ArrayList<Fragment>();
        mFragment1=new FragmentPage1();
        mFragment2=new FragmentPage2();
        mFragment3=new FragmentPage3();
        mFragment4=new FragmentPage4();
        mFragments.add(mFragment1);
        mFragments.add(mFragment2);
        mFragments.add(mFragment3);
        mFragments.add(mFragment4);
        mViewPager.setAdapter(new MyViewPagerAdapter(getSupportFragmentManager(),mFragments));
        mViewPager.setCurrentItem(0);
        mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
    }

    private void initTabs() {
        mTab1 = (TextView) findViewById(R.id.tab1);
        mTab2 = (TextView) findViewById(R.id.tab2);
        mTab3 = (TextView) findViewById(R.id.tab3);
        mTab4 = (TextView) findViewById(R.id.tab4);

        mTab1.setOnClickListener(new MyOnClickListener(0));
        mTab2.setOnClickListener(new MyOnClickListener(1));
        mTab3.setOnClickListener(new MyOnClickListener(2));
        mTab4.setOnClickListener(new MyOnClickListener(3));
    }

    private void initTabLine() {
        mTabLine= (ImageView) findViewById(R.id.tab_line);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;// 获取分辨率宽度
        bmpW = screenW/4;
        ViewGroup.LayoutParams oldParams = mTabLine.getLayoutParams();
        mTabLine.setLayoutParams(new LinearLayout.LayoutParams(bmpW, oldParams.height));

    }

    /**
     * Description: tab点击监听器
     * Author: ljgsonx
     * Time: 2016/1/15 10:47
     */
    private class MyOnClickListener implements View.OnClickListener {

        private int index=0;

        public MyOnClickListener(int i){
            index=i;
        }

        public void onClick(View v) {
            mViewPager.setCurrentItem(index);
        }

    }

    /**
     * Description: fragment适配器
     * Author: ljgsonx
     * Time: 2016/1/15 10:46
     */
    public class MyViewPagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> fragments;

        public MyViewPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }

    /**
     * Description: 页面切换监听器,用于tab下横线的移动动画
     * Author: ljgsonx
     * Time: 2016/1/15 10:46
     */
    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        public void onPageScrollStateChanged(int arg0) {}

        public void onPageScrolled(int arg0, float arg1, int arg2) {}

        public void onPageSelected(int arg0) {
            Animation animation = new TranslateAnimation(bmpW*currIndex, bmpW*arg0, 0, 0);
            currIndex = arg0;
            animation.setFillAfter(true);
            animation.setDuration(300);
            mTabLine.startAnimation(animation);
            Toast.makeText(MainActivity.this, "您选择了" + mViewPager.getCurrentItem() + "页卡", Toast.LENGTH_SHORT).show();
        }

    }

}
